package com.fll.fangyishare.service.impl;

import com.fll.fangyicommon.entity.share.Share;
import com.fll.fangyishare.dao.ShareDao;
import com.fll.fangyishare.service.*;
import com.fll.fangyishare.service.rpc.FangyiUserRpc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author fll
 * create 2023/3/20
 */
@Service
@Slf4j
public class ShareServiceImpl implements ShareService {

    @Autowired
    private ShareDao shareDao;

    @Autowired
    private OssService ossService;

    @Autowired
    private LikedService likedService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private CollectService collectService;

    @Autowired
    private FangyiUserRpc fangyiUserRpc;


    /**
     * 插入一条share
     * @param share
     * @return
     */
    @Override
    public Integer addShare(Share share){
        Integer num = shareDao.addShare(share);
        if (num != 0){
            log.info("插入share成功,记录数===>{}", num);
            return num;
        }
        log.warn("插入share失败");
        return -1;
    }

    /**
     * 获取当前登录用户的share
     * @param userId
     * @return
     */
    @Override
    public ArrayList<Share> getShareByUser(Integer userId){
        ArrayList<Share> shareList = shareDao.getShareByUser(userId);
        if (shareList.size() != 0 && shareList != null){
            log.info("查询当前用户share成功===>{}",shareList);
            /**
             * 以下均为当前登录用户的share
             */
            common(shareList, userId);
            return shareList;
        }
        log.error("查询当前用户share为空===>{}",shareList);
        return null;
    }

    /**
     * 获取当前登录用户点赞的share
     * @param shareIds
     * @return
     */
    @Override
    public ArrayList<Share> getLikedShareByUser(Integer userId, String shareIds){
        String shareIdList = likedService.getLikeByUser(userId);
        if (shareIdList != null){
            ArrayList<Share> likedShareByUser = shareDao.getLikedShareByUser(shareIdList);
            /**
             * 以下为当前登录用户点赞的share信息
             */
            common(likedShareByUser, userId);
            return likedShareByUser;
        }
        //没有点赞的share就返回null
        return null;
    }

    /**
     * 获取当前登录用户收藏的share
     * @param shareIds
     * @return
     */
    @Override
    public ArrayList<Share> getCollectShareByUser(Integer userId, String shareIds){
        String shareIdList = collectService.getCollectByUser(userId);
        if (shareIdList != null){
            ArrayList<Share> likedShareByUser = shareDao.getCollectShareByUser(shareIdList);
            common(likedShareByUser, userId);
            /**
             * 以下为当前登录用户收藏的share信息
             */
            return likedShareByUser;
        }
        //没有点赞的share就返回null
        return null;
    }

    /**
     * 获取热门share：热门share=点赞+收藏+评论总数最高
     * @param userId  传入userId是判断当前用户是否对热门share进行评论、点赞、收藏
     * @return
     */
    @Override
    public ArrayList<Share> getHotShare(Integer userId){
        ArrayList<Share> shareList = shareDao.getHotShare(userId);
        ArrayList<Share> sortShareList = null;
        if (shareList.size() != 0 && shareList != null){
            //1先对获取到的share赋值
            common(shareList, userId);
            //2把share按点赞+收藏+评论进行降序排序
            sortShareList = (ArrayList<Share>)shareList.stream().sorted(new Comparator<Share>() {
                @Override
                public int compare(Share o1, Share o2) {
                    //按热门程度排序：点赞+收藏+评论
                    return (o2.getLikedCount() + o2.getCollectCount() + o2.getCommentCount()) - (o1.getLikedCount() + o1.getCollectCount() + o1.getCommentCount());
                }
            }).collect(Collectors.toList());
            //3只返回最多10条热门share
            int length = sortShareList.size();
            if (length >= 10){
                length = 10;
            }
            List<Share> list = sortShareList.subList(0, length);
            ArrayList<Share> newShareList = new ArrayList<>(list);
            return newShareList;
        }
        return null;
    }

    /**
     * 获取好友share，先查好友user_id,再根据user_id查该好友的share
     * @param userId
     * @return
     */
    @Override
    public ArrayList<Share> getFriendShare(Integer userId){
        //1.先查出所有的好友id
        String userIdList = fangyiUserRpc.getFriendByRpc(userId);
        if (userIdList != null){
            //2.根据好友id查好友的share
            ArrayList<Share> friendShareByUser = shareDao.getFriendShare(userIdList);
            common(friendShareByUser, userId);
            /**
             * 以下为当前登录用户好友的share
             */
            return friendShareByUser;
        }
        //没有点赞的share就返回null
        return null;
    }


    public void common(ArrayList<Share> shareList, Integer userId){
        for (Share share : shareList){
            //赋值url图片
            share.setOss(ossService.getShareOss(share.getShareId()));
            //赋值是否点赞
            share.setIsLike(likedService.isLike(userId, share.getShareId()));
            //赋值用户的share被点赞总数
            share.setLikedCount(likedService.getLikedCount(share.getShareId()));
            //赋值是否评论
            share.setIsComment(commentService.isComment(userId, share.getShareId()));
            //赋值用户的share被评论总数
            share.setCommentCount(commentService.getCommentCount(share.getShareId()));
            //赋值评论内容
            share.setCommentList(commentService.getAllComment(share.getShareId(), userId));
            //赋值是否收藏
            share.setIsCollect(collectService.isCollect(userId, share.getShareId()));
            //赋值用户的share被收藏总数
            share.setCollectCount(collectService.getCollectCount(share.getShareId()));
            //赋值share所属user的user信息
            share.setUser(fangyiUserRpc.getUserInfoByUserId(share.getUserId()));
        }
    }

}
